﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CFM.API;
using CFM.DTO;
using CFM.Common;

namespace CFM
{
    public partial class frmChangePassword : Form
    {
          private UserAPI _apius = null;
        public frmChangePassword()
        {
            _apius = new UserAPI();
            InitializeComponent();
        }
        private void EnableControl(bool flag)
        {
            txtPasswordNew.Enabled = flag;
            txtPasswordOld.Enabled = flag;
        }
      
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckBeforeSave())
                return;
            User user = null;
            user = (User)Const.CurrentUser;
         
            if (txtPasswordOld.Text != user.Password)
            {
                MessageBox.Show(" Mat khau khong dung");
            }
            else
            {
                user.Password = txtPasswordNew.Text;
                bool result = _apius.ChangePass(user);
                if (result != null)
                    MessageBox.Show("thanh cong");
                else
                    MessageBox.Show("that bai");
            }
        }    
        private bool CheckBeforeSave()
        {
            bool flag = true;
            if (txtPasswordNew.Enabled && txtPasswordNew.Text.Length == 0)
            {
                MethodHelper.AddErrorProvider(txtPasswordNew, Const.EMPTY_MESSAGE);
                flag = false;
            }
            if (txtPasswordOld.Enabled && txtPasswordOld.Text.Length == 0)
            {
                MethodHelper.AddErrorProvider(txtPasswordOld, Const.EMPTY_MESSAGE);

                flag = false;
            }
            return flag;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

     
    }
}
